package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.result.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.impl.EmployeeServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.poi.util.SystemOutLogger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ Description: 部门处理器类
 * @ Author: 伍家钰
 * @ Date: 2020/7/5 17:42
 * @ Version: 1.0
 */
@Controller
@RequestMapping("/department")
public class DepartmentServiceController {
    @Autowired
    private IDepartmentService departmentService;

    //删除操作
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        // 删除员工,当id为空时,提示删除失败
        if (id != null) {
            departmentService.delete(id);
        }
        return new JsonResult();
    }

    //去新增或者修改页面
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Department department = departmentService.selectById(id);
            model.addAttribute("department", department);
        }
        return "department/input";
    }

    //新增或者修改
    @RequestMapping("/saveOrUpdate")
    public String update(Department department) {
        if (department.getId() != null) {
            departmentService.update(department);
        } else {
            departmentService.save(department);
        }
        //让浏览器重新发送一次请求
        return "redirect:/department/list.do";
    }

    /**
     * 查询操
     *
     * @param model
     * @param qo
     * @return
     */
    // 当前登入用户必须拥有admin角色才能房委会
    //@RequiresRoles("admin")
    // 当前登入用户必须拥有该权限才能访问,允许配置对个,配置多个表示必须同时满足配置的多个权限才能访问
    //@RequiresPermissions({"department:list","department:delete"})
    // 为了方便获取权限的名称,将逻辑改为"或",只要其中一个有就可以访问
    @RequiresPermissions(value = {"department:list","部门页面"},logical = Logical.OR)
    @RequestMapping("/list")
    /* @RequiredPermission(name = "部门页面", expression = "department:list")*/
    public String query(Model model, QueryObject qo) {
        // 1.获取主体对象
        Subject subject = SecurityUtils.getSubject();
        // 2.需要判断角色或权限的时候,shiro会自动realm中的doGetAuthorizationInfo,获取到用户的数据,进行匹配
        System.out.println("角色-----:" + subject.hasRole("admin"));
        System.out.println("权限-----:" + subject.isPermitted("department:list"));
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";
    }

}
